package flc.ast.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.AllReplaceActivity;
import flc.ast.activity.IconReplaceActivity;
import flc.ast.adapter.IconLibraryTabAdapter;
import flc.ast.adapter.IconSelTabAdapter;
import flc.ast.databinding.FragmentIconSelTabBinding;
import flc.ast.databinding.FragmentIconTabBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatConstant;
import stark.common.bean.StkResBean;

public class IconTabFragment extends BaseNoModelFragment<FragmentIconTabBinding> {

    private IconLibraryTabAdapter tabAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_icon_tab;
    }

    @Override
    protected int getPageType() {
        return EventStatConstant.PAGE_OTHER;
    }

    public static IconTabFragment newInstance(String url) {
        Bundle args = new Bundle();
        args.putSerializable(Content.Url, url);
        IconTabFragment fragment = new IconTabFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initView() {
        mDataBinding.ivUseAll.setOnClickListener(this);
        //tab
        mDataBinding.rvTabList.setLayoutManager(new GridLayoutManager(mContext, 4));
        tabAdapter = new IconLibraryTabAdapter();
        mDataBinding.rvTabList.setAdapter(tabAdapter);
        tabAdapter.setOnItemClickListener(this);
        tabAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivUseAll:
                AllReplaceActivity.sIconList=tabAdapter.getData();
                startActivity(AllReplaceActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        IconReplaceActivity.sSelIcon=tabAdapter.getItem(position).getUrl();
        IconReplaceActivity.type=1;
        startActivity(IconReplaceActivity.class);
    }

    @Override
    protected void initData() {
        String url = getArguments().getString(Content.Url);
        getData(url);
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(null, url, StkApi.createParamMap(0, 28), true,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                tabAdapter.setList(result);
                            }
                        }
                    }
                });
    }
}
